package com.xr.system;

import com.bstek.dorado.common.*;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.data.db.SqlDataset;
import com.bstek.dorado.view.*;
import com.bstek.dorado.view.data.*;
import com.bstek.dorado.view.control.*;
import com.xr.common.DBTools;
import com.xr.common.Tools;
import java.util.*;

/**
 * employeeRoleViewModel
 */
public class employeeRoleViewModel extends DefaultViewModel {
	public void saveRole(ParameterSet parameters) throws Exception {
		Dataset employeeRoleDataset = getDataset("employeeRoleDataset");
		Dataset orgEmployeeDataset = getDataset("orgEmployeeDataset");
		HashMap functionRightMap=new HashMap();
		RecordIterator iterator=employeeRoleDataset.recordIterator();		
		iterator.setVisibility(employeeRoleDataset.FILTER_ALL);
		DBStatement statement = new DBStatement();
		while (iterator.hasNext()) {
			Record emmRoleRecord=iterator.nextRecord();			
			String roleInsId=Tools.toStr(emmRoleRecord.getString("ROLE_INS_ID"));
			String employeeId=Tools.toStr(orgEmployeeDataset.getString("EMPLOYEE_ID"));
			SqlDataset userDataset = new SqlDataset();
			userDataset.setSql("select user_id from rt_user where for_employee_id="+employeeId);
			userDataset.load(true);
			userDataset.moveFirst();
			String userId=userDataset.getString("user_id");

			if(emmRoleRecord.getState()==Record.STATE_NONE){					
				   SqlDataset functionTemplateDataset = new SqlDataset();
				   functionTemplateDataset.setSql("select fr.function_id from rt_function_template ft,rt_function_template_right fr " +
							"where ft.function_template_id=fr.function_template_id and ft.role_type="+roleInsId);
				   functionTemplateDataset.setPageSize(0);
				   functionTemplateDataset.load(true);
				   
				   functionTemplateDataset.moveFirst();
				   while (!functionTemplateDataset.isLast())  {
					   String fun_id=functionTemplateDataset.getString("function_id");
                       if(!functionRightMap.containsKey(fun_id)){
                    	   functionRightMap.put(fun_id, ""); 
                       }
   					statement.setSql("delete from rt_function_right where function_id="
							+functionTemplateDataset.getString("function_id")+
							" and user_id = "+userId);
					statement.execute();
					statement.close();					
					statement.setSql("insert into rt_function_right(function_id,user_id) values ("
							+functionTemplateDataset.getString("function_id")+
							","+userId+")");
					statement.execute();
					statement.close();
						functionTemplateDataset.moveNext();
					}			   
				}			
			
			if(emmRoleRecord.getState()==Record.STATE_NEW){
			   String feEmployeeRoleId = DBTools.getNewId("general");
			   emmRoleRecord.setValue("PVD_EMP_ROLE_ID", feEmployeeRoleId);
			   statement.setSql("insert into rt_pvd_emp_role(pvd_emp_role_id,role_ins_id,employee_id) values("
  				    +feEmployeeRoleId+","+Tools.toStr(emmRoleRecord.getString("ROLE_INS_ID"))+","
				    +employeeId+")");
			   statement.execute();
			   statement.close();
				
			   SqlDataset functionTemplateDataset = new SqlDataset();
			   functionTemplateDataset.setSql("select fr.function_id from rt_function_template ft,rt_function_template_right fr " +
						"where ft.function_template_id=fr.function_template_id and ft.role_type="+roleInsId);
			   functionTemplateDataset.setPageSize(0);
			   functionTemplateDataset.load(true);
			   functionTemplateDataset.moveFirst();
			   
			   while (!functionTemplateDataset.isLast())  {
				   String fun_id=functionTemplateDataset.getString("function_id");
                   if(!functionRightMap.containsKey(fun_id)){
                	   functionRightMap.put(fun_id, ""); 
                   }
					statement.setSql("delete from rt_function_right where function_id="
							+functionTemplateDataset.getString("function_id")+
							" and user_id = "+userId);
					statement.execute();
					statement.close();					
					statement.setSql("insert into rt_function_right(function_id,user_id) values ("
							+functionTemplateDataset.getString("function_id")+
							","+userId+")");
					statement.execute();
					statement.close();
					
					functionTemplateDataset.moveNext();
				}			   
			}
			if(emmRoleRecord.getState()==Record.STATE_MODIFIED){
				ReadRecord oldEmmRoleRecord=employeeRoleDataset.getOldRecord(emmRoleRecord);
				String oldRoleInsId=Tools.toStr(oldEmmRoleRecord.getString("ROLE_INS_ID"));
				String feEmployeeRoleId = emmRoleRecord.getString("PVD_EMP_ROLE_ID");
				statement.setSql("update  rt_pvd_emp_role set role_ins_id='"+roleInsId+"' where pvd_emp_role_id='"+feEmployeeRoleId+"'");					    
			    statement.execute();
				statement.close();	
				SqlDataset oldFunctionTemplateDataset = new SqlDataset();
				oldFunctionTemplateDataset.setSql("select fr.function_id from rt_function_template ft,rt_function_template_right fr " +
							"where ft.function_template_id=fr.function_template_id and ft.role_type="+oldRoleInsId);
				oldFunctionTemplateDataset.setPageSize(0);
				oldFunctionTemplateDataset.load(true);
				
				while (!oldFunctionTemplateDataset.isLast())  {
					String fun_id=oldFunctionTemplateDataset.getString("function_id");
					if(!functionRightMap.containsKey(fun_id)){
				    statement.setSql("delete from rt_function_right where function_id="
								+oldFunctionTemplateDataset.getString("function_id")+
								" and user_id = "+userId);
				    statement.execute();
					statement.close();	
					}
					oldFunctionTemplateDataset.moveNext();
				 }	
				 
				SqlDataset functionTemplateDataset = new SqlDataset();
				functionTemplateDataset.setSql("select fr.function_id from rt_function_template ft,rt_function_template_right fr " +
							"where ft.function_template_id=fr.function_template_id and ft.role_type="+roleInsId);
				functionTemplateDataset.setPageSize(0);
				functionTemplateDataset.load(true);
				
				while (!functionTemplateDataset.isLast())  {
				   String fun_id=functionTemplateDataset.getString("function_id");
                   if(!functionRightMap.containsKey(fun_id)){
                	   functionRightMap.put(fun_id, ""); 
                   }
					statement.setSql("delete from rt_function_right where function_id="
								+functionTemplateDataset.getString("function_id")+
								" and user_id = "+userId);
					statement.execute();
					statement.close();					
					statement.setSql("insert into rt_function_right(function_id,user_id) values ("
								+functionTemplateDataset.getString("function_id")+
								","+userId+")");
					statement.execute();
					statement.close();
						
				functionTemplateDataset.moveNext();
			    }				
			}

			if(emmRoleRecord.getState()==Record.STATE_DELETED){
				String feEmployeeRoleId = emmRoleRecord.getString("PVD_EMP_ROLE_ID");
				SqlDataset functionTemplateDataset = new SqlDataset();
				functionTemplateDataset.setSql("select fr.function_id from rt_function_template ft,rt_function_template_right fr " +
						"where ft.function_template_id=fr.function_template_id and ft.role_type="+roleInsId);
				functionTemplateDataset.setPageSize(0);
				functionTemplateDataset.load(true);
			
				while (!functionTemplateDataset.isLast())  {
					String fun_id=functionTemplateDataset.getString("function_id");
					if(!functionRightMap.containsKey(fun_id)){
					statement.setSql("delete from rt_function_right where function_id="
							+functionTemplateDataset.getString("function_id")+
							" and user_id = "+userId);
					statement.execute();
					statement.close();
					}
					functionTemplateDataset.moveNext();
				}
				
				statement.setSql("delete from rt_pvd_emp_role where PVD_EMP_ROLE_ID="
						+feEmployeeRoleId);
				statement.execute();
				statement.close();				
				
			}
				
		}
		
			
	}

}
